// pages/ticket/list/list.js
import {
  TicketModel
} from '../../../models/ticket'
const ticketModel = new TicketModel()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        title: '未使用',
      },
      {
        title: '已使用',
      },
      {
        title: '已失效'
      }
    ],
    activeTab: 0,
    // list: '默认值',
    page: 0,
    show: false, 
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.myLogin().then(res => {
      wx.hideLoading();
      this.loadMyticket(0)
    })
  },
  loadMyticket(begin) {
    let vm = this
    ticketModel.loadMyticket(this.data.activeTab+1,begin,5).then(res => {
      if (vm.data.page == 0) {
        vm.setData({
          list: res.data
        })
      } else {
        if (res.num > this.data.list.length) {
          let list = this.data.list;
          for (let i = 0; i < res.data.length; i++) {
            list.push(res.data[i]);
          }
          vm.setData({
            list,
            show: false
          });
        } else {
          vm.setData({
            show: false,
          });
        }
        
      }
    })
  },
  changeList() {
    const activeTab = this.data.activeTab
    let list = []
    const listAll = this.data.listAll
    if (activeTab == 0) {
      list = listAll.unuseList
    } else if (activeTab == 1) {
      list = listAll.usedList
    } else if (activeTab == 2) {
      list = listAll.overdueList
    }
    this.setData({
      list
    })
  },
  scrollMytrip2() {
    try{
      this.setData({ show: true });
      this.data.page = this.data.page + 1
      this.loadMyticket(this.data.page)
    } catch(e) {
      this.setData({ show: false });
      console.log('scrollMytrip执行异常:', e);
    }
  },
  toDetail(e){
    console.log(e)
    const d = e.currentTarget.dataset
    let innerOrgCode = ''
    if (app.globalData.station.innerOrgCode) {
      innerOrgCode = app.globalData.station.innerOrgCode
    }
    if (d.item.couponType == 2) {
      return
    }
    if (this.data.activeTab == 0) {
      ticketModel.checkCqVirtualGood(d.entityNum,innerOrgCode).then(res => {
        if (res) {
          if (res.code == '00') {
            if (d.item.type == 3) {
              wx.navigateTo({
                url: '/pages/ticket/ticketDetail/ticketDetail?ticketId=' + d.item.id
              })
              return
            }
            wx.reLaunch({
              url: '/pages/confirm/confirm?entityNum='+d.entityNum
            })
          } else {
            wx.showToast({
              type: 'none',
              content: res.msg,
              duration: 1000,
            });
          }
        } else {
          wx.showToast({
            type: 'none',
            content: '校验优惠券是否可用失败，请稍后再试',
            duration: 1000,
          });
        }
      })
      // wx.reLaunch({
      //   url: '/pages/confirm/confirm?entityNum='+d.entityNum
      // })
    }
    
  },
  tabChange(index) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
    });
    this.loadMyticket(0)
  },
  handleTabClick({
    index
  }) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
    });
    this.loadMyticket(0)
  },
  handleTabChange({
    index
  }) {
    this.setData({
      activeTab: index,
      page: 0,
      list: '',
    });
    this.loadMyticket(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})